package com.itc.dispatch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itc.common.core.utils.OkHttpUtil;
import com.itc.common.core.utils.StringUtils;
import com.itc.dispatch.domain.OaDept;
import com.itc.dispatch.domain.OaUser;
import com.itc.dispatch.domain.vo.OaUserTreeVo;
import com.itc.dispatch.service.OaUserService;
import com.itc.dispatch.mapper.OaUserMapper;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author 91516
* @description 针对表【oa_user】的数据库操作Service实现
* @createDate 2024-09-24 13:29:29
*/
@Service
public class OaUserServiceImpl extends ServiceImpl<OaUserMapper, OaUser>
    implements OaUserService{

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    @Transactional
    public void oaUserDataUpdate() {
        //api获取OA用户数据
        Map<String, String> headers = new HashedMap<>();
        headers.put("X-HW-ID", "airport_pfoca_app");
        headers.put("X-HW-APPKEY","Huawei@123!@#");
        Object resultObj = OkHttpUtil.sendOkHttpClient("http://10.81.35.129/api/oaUser/data"
                , "GET",null,headers);
        if(resultObj == null){
            return;
        }
        JSONObject dataJson = JSONObject.parseObject(resultObj.toString());
        String dataList = dataJson.getString("data");
        if(StringUtils.isBlank(dataList)){
            System.out.println(dataList);
            return;
        }
        try{
            List<OaUser> deptList = objectMapper.readValue(dataList
                    ,new TypeReference<List<OaUser>>(){});
            this.baseMapper.delete(new LambdaQueryWrapper<>());
            this.saveOrUpdateBatch(deptList,500);

        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    @Override
    public List<OaUserTreeVo> getUserTreeList() {
        return this.baseMapper.getUserTreeList();
    }
}




